package com.proj.hot.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.proj.hot.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 订单
 * 
 */
public class OrderListAdapter extends BaseAdapter
{
	private List<JSONObject> arrayList;
	private Context context;
	
	public OrderListAdapter(Context context, List<JSONObject> arrayList)
	{
		if (arrayList == null)
		{
			this.arrayList = new ArrayList<JSONObject>();
		}
		else
		{
			this.arrayList = arrayList;
		}
		this.context = context;
	}
	
	@Override
	public int getCount()
	{
		return arrayList.size();
	}
	
	@Override
	public Object getItem(int position)
	{
		return arrayList.get(position);
	}
	
	public void setList(List<JSONObject> list)
	{
		if (list == null)
		{
			this.arrayList = new ArrayList<JSONObject>();
		}
		else
		{
			this.arrayList = list;
		}
	}
	
	@Override
	public long getItemId(int position)
	{
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder = null;
		if (convertView == null)
		{
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.order_list_item, null);
			holder.order_name = (TextView) convertView.findViewById(R.id.order_name);
			holder.order_surplus = (TextView) convertView.findViewById(R.id.order_surplus);
			holder.order_price = (TextView) convertView.findViewById(R.id.order_price);
			holder.order_paytag = (TextView) convertView.findViewById(R.id.order_paytag);
			holder.order_buytime = (TextView) convertView.findViewById(R.id.order_buytime);
			//holder.order_validtime = (TextView) convertView.findViewById(R.id.order_validtime);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			holder.order_name.setText(arrayList.get(position).getString("NAME"));
			holder.order_surplus.setText(arrayList.get(position).getString("usefultime"));
			holder.order_price.setText(arrayList.get(position).getString("price"));
			holder.order_paytag.setText(arrayList.get(position).getString("paytype"));
			holder.order_buytime.setText(arrayList.get(position).getString("createtime"));
			holder.order_validtime.setText(arrayList.get(position).getString("surplustime"));
		}catch (Exception e){
			e.printStackTrace();
		}

		return convertView;
	}
	
	class ViewHolder
	{
		private TextView order_name;
		private TextView order_surplus;
		private TextView order_price;
		private TextView order_paytag;
		private TextView order_buytime;
		private TextView order_validtime;

	}
}
